﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using CourseArraging.Common;
using CourseArraging.Frm.jxgl;
using CourseArraging.Frm.jygl;
using CourseArraging.Frm.bbxx;
using Stone.Library.Common;
using Stone.DataContracts;

namespace CourseArraging.Frm.xygl
{
    /// <summary>
    /// 版权所有
    /// 石世特@温州医学院 sst330381@gmail.com
    /// </summary> 
    public partial class FrmStuStatistics : FrmToolbarBase
    {
        #region 字段属性
        private int currentRowIndex = -1;
        private int currentColumnIndex = -1;
        private int currentTabIndex = -1;
        private LsapEntity lsapEntity = new LsapEntity();

        YhxxService.YhxxServiceClient yhxxdao =new YhxxService.YhxxServiceClient();
        LsapService.LsapServiceClient lsapdao = new LsapService.LsapServiceClient();
        private static FrmStuStatistics instance;
        #endregion

        #region 构造器
        public static FrmStuStatistics GetInstance()
        {
            if (instance == null || instance.IsDisposed)
            {
                instance = new FrmStuStatistics();
                instance.MdiParent = GlobalVar.frmMain;
            }
            instance.BringToFront();
            return instance;
        }
        public FrmStuStatistics()
        {
            InitializeComponent();
            this.multiColHeaderDgv1.CellDoubleClick += new DataGridViewCellEventHandler(multiColHeaderDgv1_CellDoubleClick);
            this.multiColHeaderDgv1.CellClick += new DataGridViewCellEventHandler(multiColHeaderDgv1_CellClick);
            showData(getDateSource());
             
            //让cell自动根据内容换行
            this.multiColHeaderDgv1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;
        }

        private List<StuArrangeEntity> getDateSource()
        {
            return yhxxdao.GetStuArrangeList().ToList<StuArrangeEntity>();
        }
        #endregion

        #region 事件

        private void FrmStuStatistics_Shown(object sender, EventArgs e)
        {
            warnRows();     
            //alterColumns();// 取消列的颜色加载
        }

        void multiColHeaderDgv1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
            this.currentRowIndex = e.RowIndex;
            this.currentColumnIndex = e.ColumnIndex;
            this.currentTabIndex = 1;
        }
         
        void multiColHeaderDgv1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (string.IsNullOrEmpty(multiColHeaderDgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
            {
                FrmLsap2 frm = new FrmLsap2(new LsapEntity()
                {
                    lsap_xyxm = multiColHeaderDgv1.Rows[e.RowIndex].Cells["name"].Value.ToString(),
                    //lsap_kcsj = "6-" + multiColHeaderDgv1.Columns[e.ColumnIndex].HeaderText,
                    lsap_kcsj = multiColHeaderDgv1.Columns[e.ColumnIndex].HeaderText,
                    lsap_xybh = multiColHeaderDgv1.Rows[e.RowIndex].Cells["no"].Value.ToString()
                });
                frm.lsapevent += new FrmLsap2.lsapDelegate(frm_lsapevent);
                frm.ShowDialog();
            }
        }

        
        void frm_lsapevent()
        {
            showData(getDateSource());
        }

        #endregion

        #region 实现抽象方法

        public override void tsb_del_fun()
        {
            if (currentRowIndex == -1 || currentColumnIndex == -1 || currentTabIndex == -1) return;
            if (currentTabIndex == 1)
            {
                lsapEntity.lsap_jybh = multiColHeaderDgv1.Rows[currentRowIndex].Cells["no"].Value.ToString();
                lsapEntity.lsap_km = multiColHeaderDgv1.Rows[currentRowIndex].Cells[currentColumnIndex]
                    .Value.ToString().Split('-')[0];
                lsapEntity.lsap_kcsj = "6-" + multiColHeaderDgv1.Columns[currentColumnIndex].HeaderText;
                lsapdao.UpdateZT(lsapEntity);
            }
            showData(getDateSource());
        }

        public override void tsb_refresh_fun()
        {
            showData(getDateSource());
        }

        public override void tsb_print_fun()
        {
            DataTable dt = this.multiColHeaderDgv1.DataSource as DataTable;
            FrmStuZmap bb = new FrmStuZmap(dt);
            bb.ShowDialog();
        }

        public override void tsb_search_fun()
        {
            FrmStuZmCondi frm = FrmStuZmCondi.getInstance(FrmStuZmCondi.FrmMode.FRM_STU_ZMAP);
            frm.FilterEvent += new FrmStuZmCondi.FilterEventHandler(frm_FilterEvent);
            frm.ShowDialog();
        }

        void frm_FilterEvent(List<StuArrangeEntity> sList)
        {
            showData(sList);
        }
        #endregion

        #region 方法

        private void showData(List<StuArrangeEntity> saeList)
        {
            #region 6&7
            DataTable dtable1 = new DataTable();
            dtable1.Columns.Add("name", typeof(System.String));
            dtable1.Columns.Add("no", typeof(System.String));
            dtable1.Columns.Add("6-8:00-10:00", typeof(System.String));//上午四节
            dtable1.Columns.Add("6-10:00-12:00", typeof(System.String));
            dtable1.Columns.Add("6-13:00-15:00", typeof(System.String));//下午四节
            dtable1.Columns.Add("6-15:00-17:00", typeof(System.String));
            dtable1.Columns.Add("6-18:00-20:00", typeof(System.String));//晚上四节
            dtable1.Columns.Add("6-20:00-22:00", typeof(System.String));

            dtable1.Columns.Add("7-8:00-10:00", typeof(System.String));//上午四节
            dtable1.Columns.Add("7-10:00-12:00", typeof(System.String));
            dtable1.Columns.Add("7-13:00-15:00", typeof(System.String));//下午四节
            dtable1.Columns.Add("7-15:00-17:00", typeof(System.String));
            dtable1.Columns.Add("7-18:00-20:00", typeof(System.String));//晚上四节
            dtable1.Columns.Add("7-20:00-22:00", typeof(System.String));

            if (saeList != null)
            {
                DataRow drow;
                for (int i = 0; i < saeList.Count; i++)
                {
                    #region 添加行
                    drow = dtable1.NewRow();
                    drow["name"] = saeList[i].user_name;
                    drow["no"] = saeList[i].user_no;
                    drow["6-8:00-10:00"] = "";
                    drow["6-10:00-12:00"] = "";
                    drow["6-13:00-15:00"] = "";
                    drow["6-15:00-17:00"] = "";
                    drow["6-18:00-20:00"] = "";
                    drow["6-20:00-22:00"] = "";

                    drow["7-8:00-10:00"] = "";
                    drow["7-10:00-12:00"] = "";
                    drow["7-13:00-15:00"] = "";
                    drow["7-15:00-17:00"] = "";
                    drow["7-18:00-20:00"] = "";
                    drow["7-20:00-22:00"] = "";
                    if (!string.IsNullOrEmpty(saeList[i].courseEntity.course_time))
                    {
                        string[] tmp = saeList[i].courseEntity.course_time.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int j = 0; j < tmp.Length; j++)
                        {
                            switch (CommonHelper.course2multi(tmp[j]))
                            {
                                #region 判断赋值
                                case 61:
                                    drow["6-8:00-10:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 63:
                                    drow["6-10:00-12:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 65:
                                    drow["6-13:00-15:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 67:
                                    drow["6-15:00-17:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 69:
                                    drow["6-18:00-20:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 611:
                                    drow["6-20:00-22:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;

                                case 71:
                                    drow["7-8:00-10:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 73:
                                    drow["7-10:00-12:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 75:
                                    drow["7-13:00-15:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;

                                case 77:
                                    drow["7-15:00-17:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 79:
                                    drow["7-18:00-20:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;
                                case 711:
                                    drow["7-20:00-22:00"] = saeList[i].courseEntity.course_name + "-" + saeList[i].tecEntity.user_name + "\r\n" + saeList[i].user_bz;
                                    break;


                                #endregion
                            }
                        }
                    }
                    dtable1.Rows.Add(drow);
                    #endregion
                }
                #region 合并同名行(修改列数之后需要修改j变量)
                for (int i = 0; i < dtable1.Rows.Count - 1; i++)
                {
                    DataRow dr = dtable1.Rows[i];
                    if (dr["name"].ToString() == dtable1.Rows[i + 1]["name"].ToString())
                    {
                        for (int j = 2; j < dtable1.Columns.Count; j++)
                        {
                            dr[j] += dtable1.Rows[i + 1][j].ToString();
                        }
                        dtable1.Rows.RemoveAt(i + 1);
                        i--;
                    }
                }
                #endregion
            }  
            
            #endregion
            multiColHeaderDgv1.DataSource = dtable1;
            multiColHeaderDgv1.Columns["no"].Visible = false;
            warnRows();
        }

        private void alterColumns()
        {
            int tmp;
            for (int i = 0; i<multiColHeaderDgv1.ColumnCount; i++)
            { 
                tmp=i%4;
                if (tmp == 3 || tmp == 2)
                {
                    multiColHeaderDgv1.Columns[i].DefaultCellStyle
                        .BackColor = Color.Wheat;
                }
            }
        }

        private void warnRows()
        {
            for (int i = 0; i < multiColHeaderDgv1.Rows.Count; i++)
            {
                int course_count = 0;
                for (int j = 2; j < multiColHeaderDgv1.ColumnCount; j++)
                {
                    if (!string.IsNullOrEmpty(multiColHeaderDgv1.Rows[i].Cells[j].Value.ToString()))
                        course_count++;
                }
                if (course_count <= 1)
                {
                    multiColHeaderDgv1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                }
                if (course_count >= 4)
                {
                    multiColHeaderDgv1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
            }
        }
        #endregion


    }
}
